from django.contrib import messages
from django.http import HttpResponse
from django.shortcuts import render
from django.core.paginator import Paginator
from django.views.decorators.csrf import csrf_exempt
from content.models import UserRating
from manager.forms.ratingform import RatingForm,RatingSearchForm
from common import res_json_data
import json



@csrf_exempt
def rating_manage(request):
    search = RatingSearchForm()
    return render(request, 'manager/rating_manage/index.html', {'search': search})

@csrf_exempt
def rating_data(request):
    if request.method == 'POST':
        limit = request.POST.get('limit')
        page = request.POST.get('page')
        searchParams = request.POST.get('searchParams')
        if searchParams != None:
            searchParams = json.loads(searchParams)
            user = searchParams.get('user', None)
            goods = searchParams.get('goods', None)
            negotiation = UserRating.objects.all()
            if user:
                negotiation = negotiation.filter(user_id=user).order_by('-id')
            if goods:
                negotiation = negotiation.filter(goods_id=goods).order_by('-id')
            page_list = Paginator(negotiation, limit).page(page)
        else:
            negotiation = UserRating.objects.all().order_by('-id')
            page_list = Paginator(negotiation, limit).page(page)
        data = []
        index = (int(page) - 1) * int(limit)
        for i in page_list:
            item_dic = {}
            index += 1
            item_dic['index'] = index
            field_names = [f.name for f in i._meta.get_fields()]
            # print('字段名', field_names)
            for key in field_names:
                if key == 'user':
                    item_dic[key] = getattr(i, key).username
                elif key == 'goods':
                    item_dic[key] = getattr(i, key).name
                else:
                    item_dic[key] = getattr(i, key)
            data.append(item_dic)
        # print('===============data==',data)
        return res_json_data.table_api(data=data, count=len(negotiation))

@csrf_exempt
def rating_add(request):
    if request.method == 'GET':
        form = RatingForm()
        print('==========form',form)
        return render(request, 'common/edit.html', {'form': form})
    form = RatingForm(data=request.POST)
    if form.is_valid():
        form.save()
        messages.success(request, '添加成功')
        return render(request, 'common/edit.html', {'form': form})
    return render(request, 'common/edit.html', {'form': form})


@csrf_exempt
def rating_edit(request):
    id = request.GET.get('id')
    user_instance = UserRating.objects.get(id=id)
    if request.method == 'GET':
        form = RatingForm(instance=user_instance)
        return render(request, 'common/edit.html', {'form': form})
    form = RatingForm(data=request.POST, instance=user_instance)
    if form.is_valid():
        form.save()
        messages.success(request, '编辑成功')
        return render(request, 'common/edit.html', {'form': form})

    return render(request, 'common/edit.html', {'form': form})

@csrf_exempt
def rating_delete(request):
    if request.method == 'POST':
        id = request.POST.get('id')
        UserRating.objects.get(id=id).delete()
        return HttpResponse('ok')

